// 选择充值金额
const rechargeOptions = document.querySelectorAll('.recharge-option');
const submitBtn = document.querySelector('.submit-btn');

let selectedAmount = null;
let selectedMethod = 'alipay';

rechargeOptions.forEach(option => {
    option.addEventListener('click', () => {
        // 移除所有选项的选中状态
        rechargeOptions.forEach(opt => opt.classList.remove('selected'));

        // 添加当前选项的选中状态
        option.classList.add('selected');

        // 更新选中的金额
        selectedAmount = option.getAttribute('data-amount');

        // 启用提交按钮
        submitBtn.disabled = false;
    });
});


document.addEventListener('DOMContentLoaded', function() {
    // 生成订单号
    generateOrderNo();

    // 支付按钮点击事件
    if(document.getElementById('payButton') != null){
        document.getElementById('payButton').addEventListener('click', pay);
    }else{
        console.log("没找到支付按钮");
    }
});

// 支付宝支付对象
const AliPayPojo = {
    widbody: '',
    widout_trade_no: '',
    widsubject: '',
    widtotal_amount: ''
};

// 支付方法
function pay() {
    // 获取表单数据
    AliPayPojo.widbody = document.getElementById('widbody').value;
    AliPayPojo.widout_trade_no = document.getElementById('widout_trade_no').value;
    AliPayPojo.widsubject = `您选择了充值 ${selectedAmount}元，支付方式: 支付宝`;
    AliPayPojo.widtotal_amount = selectedAmount;

    // 发送请求到后端
    console.log('发送支付请求:', AliPayPojo);
    axios.post("/AVR/system/admin/aliPay",AliPayPojo).then(res=> {
        if(res){
            // 实际应用中这里应该是从后端获取的响应
            const mockAlipayResponse = res;
            // 渲染支付宝返回的HTML
            document.getElementById('payDiv').innerHTML = mockAlipayResponse.data;
            // 提交表单
            setTimeout(() => {
                const payForm = document.getElementsByName('punchout_form')[0];
                if (payForm) {
                    payForm.submit();
                }
            }, 0);
        }
    })
}

// 生成订单编号
function generateOrderNo() {
    const vNow = new Date();
    let sNow = "";
    sNow += String(vNow.getFullYear());
    sNow += String(vNow.getMonth() + 1).padStart(2, '0');
    sNow += String(vNow.getDate()).padStart(2, '0');
    sNow += String(vNow.getHours()).padStart(2, '0');
    sNow += String(vNow.getMinutes()).padStart(2, '0');
    sNow += String(vNow.getSeconds()).padStart(2, '0');
    sNow += String(vNow.getMilliseconds()).padStart(3, '0');

    document.getElementById('widout_trade_no').value = sNow;
    AliPayPojo.widout_trade_no = sNow;
}